package org.example.controller;

import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.example.Main;
import org.example.dao.SettingsRepository;
import org.example.model.Settings;
import org.example.model.SettingsEntity;
import org.example.utils.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @Author: wwy
 * @Date: 2020/12/2 16:58
 */
public class SettingsDialogViewController implements Initializable {
    private static final Logger LOGGER = LoggerFactory.getLogger(SettingsDialogViewController.class);
    private static SettingsEntity currentSettings;
    private static Stage dialogStage;
    public static boolean flag = false;
    public static ApplicationContext applicationContext;
    @FXML
    private TextField planIntervalField;
    @FXML
    private TextField timeOutField;
    @FXML
    private TextField startIntervalField;
    @FXML
    private RadioButton startRadioButton;
    @FXML
    private RadioButton closeRadioButton;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ToggleGroup tg = new ToggleGroup();
        startRadioButton.setToggleGroup(tg);
        closeRadioButton.setToggleGroup(tg);
        LOGGER.info("setting初始化完成");
        if(currentSettings != null){
            planIntervalField.setText(currentSettings.getPlanInterval());
            timeOutField.setText(currentSettings.getTimeOut());
            startIntervalField.setText(currentSettings.getStartInterval());
            int isAutoLoad = currentSettings.getIsAutoLoad();
            if(isAutoLoad == 0){
                //不加载
                closeRadioButton.setSelected(true);
            }else{
                //加载
                startRadioButton.setSelected(true);
            }
        }else{
            closeRadioButton.setSelected(true);
        }
    }

    /**
     * 展示设置弹出框
     */
    public boolean showSettingsDialog(SettingsEntity settings){
        // Load the fxml file and create a new stage for the popup dialog.
        currentSettings = settings;
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/SettingsDialogView.fxml"));
        try {
            AnchorPane page = (AnchorPane) loader.load();
            // Create the dialog Stage.
            dialogStage = new Stage();
            dialogStage.setTitle("Settings");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(Main.getStage());
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
            dialogStage.showAndWait();
            return flag;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @FXML
    public void handleOk(){
        SettingsRepository settingsRepository = applicationContext.getBean(SettingsRepository.class);
        //新增
        dialogStage.close();
        flag = true;
        boolean selected = startRadioButton.isSelected();
        if(currentSettings == null){
            currentSettings = new SettingsEntity();
            currentSettings.setId(IdWorker.getNextId());
            currentSettings.setPlanInterval(planIntervalField.getText());
            currentSettings.setStartInterval(startIntervalField.getText());
            currentSettings.setTimeOut(timeOutField.getText());
        }else{
            currentSettings.setPlanInterval(planIntervalField.getText());
            currentSettings.setStartInterval(startIntervalField.getText());
            currentSettings.setTimeOut(timeOutField.getText());
        }
        if(selected){
            currentSettings.setIsAutoLoad(1);
        }else{
            currentSettings.setIsAutoLoad(0);
        }
        settingsRepository.save(currentSettings);

    }
    @FXML
    public void handleCancel(){
        dialogStage.close();
        flag = false;
    }
}
